package com.hk.yajin.function.videos.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.hk.developmentlib.utills.FastjsonUtils;
import com.hk.developmentlib.utills.NetworkInfoUtils;
import com.hk.yajin.R;
import com.hk.yajin.base.BaseActivity;
import com.hk.yajin.model.News;
import com.hk.yajin.model.ViewPorint;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ViewPorintDetailActivity extends BaseActivity {

    @BindView(R.id.lly_back)
    LinearLayout llyBack;
    @BindView(R.id.lly_share)
    LinearLayout llyShare;
    @BindView(R.id.webView)
    WebView webView;
    private ViewPorint thisItem;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article_detail);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        dataString = getIntent().getStringExtra(DATA_STRING);
        thisItem = FastjsonUtils.getBeanObject(dataString, ViewPorint.class);
        if (thisItem != null) {
            initWebView();
        } else {
            showShortToast("url不存在");
        }

    }

    private void initWebView() {

        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setCacheMode(NetworkInfoUtils.getWebCacheMode(mContext)); // 设置缓存模式
        webView.getSettings().setJavaScriptEnabled(true);// 开启JS兼容
        webView.getSettings().setDisplayZoomControls(false);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); // 设置滚动条不可见
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);  // 自适应屏幕
        webView.getSettings().setLoadWithOverviewMode(true);// 设置加载进来的页面自适应手机屏幕
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setSupportZoom(true); // 开启缩放
        webView.getSettings().setBuiltInZoomControls(true);// 开启缩放功能
        MyWebViewClient wbClient = new MyWebViewClient();
        webView.setWebViewClient(wbClient);
        // 设置处理解析，渲染网页等浏览器等事件 这里主要是设置进度条和文本
        // webView.addJavascriptInterface(new DemoJavaScriptInterface(), "callAndroid");

        webView.loadUrl(thisItem.getDetail_url());
    }

    @OnClick({R.id.lly_back, R.id.lly_share})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lly_back:
                finish();
                break;
            case R.id.lly_share:
                UMWeb shareUMVideo = new UMWeb(thisItem.getDetail_url());
                shareUMVideo.setTitle(thisItem.getView_title());
                shareUMVideo.setThumb(new UMImage(this, thisItem.getView_cover()));
                shareUMVideo.setDescription(thisItem.getView_brief());
                new ShareAction(this).withMedia(shareUMVideo)
                        .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE)
                        .setCallback(new UMShareListener() {
                            @Override
                            public void onStart(SHARE_MEDIA share_media) {

                            }

                            @Override
                            public void onResult(SHARE_MEDIA share_media) {

                            }

                            @Override
                            public void onError(SHARE_MEDIA share_media, Throwable throwable) {

                                if (throwable != null) {
                                    showShortToast(throwable.getMessage());
                                }
                            }

                            @Override
                            public void onCancel(SHARE_MEDIA share_media) {
                                showShortToast("onCancel");
                            }
                        }).open();
                break;
        }
    }

    class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return false;
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webView != null) {
            webView.destroy();
        }
    }

}
